﻿/*========================== 
 * @author 郑金泉
 * @desc 查询信息类
 * ========================= */
using System;
using System.Collections.Generic;
using System.Text;

namespace Obsidian.Edm
{
    public class QueryInfo : OModel
    {

        private IntField pageNo;
        private IntField pageSize;
        private BoolField isPaging;

        public QueryInfo()
        {
            this.InitModel(new IModelField[] { });
        }

        public void InitModel(IModelField[] fields)
        {
            List<IModelField> fieldList = new List<IModelField>(fields);
            fieldList.Add(pageNo = new IntField(this, null, "pageNo", 1));
            fieldList.Add(pageSize = new IntField(this, null, "pageSize"));
            fieldList.Add(isPaging = new BoolField(this, null, "isPaging", true));
            base.InitModel(null, null, fieldList.ToArray());
        }

        /// <summary>
        /// 页码
        /// </summary>
        public IntField PageNo { get { return pageNo; } }

        /// <summary>
        /// 页大小
        /// </summary>
        public IntField PageSize { get { return pageSize; } }

        /// <summary>
        /// 是否进行分页
        /// </summary>
        public BoolField IsPaging { get { return isPaging; } }

        /// <summary>
        /// 检查分页属性
        /// </summary>
        /// <param name="defaultPageSize">默认每页项数量</param>
        /// <param name="maxPageSize">最大每页项数量</param>
        /// <param name="msg">错误信息</param>
        /// <returns></returns>
        public bool CheckPagingAttrs(int defaultPageSize, int maxPageSize, out string msg)
        {
            if (defaultPageSize < 1)
                throw new Exception("默认每页项数量不能小于1");
            if (maxPageSize < 1)
                throw new Exception("最大每页项数量不能小于1");

            if (pageNo.IsNull)
                pageNo.Set(1);
            if (pageSize.IsNull)
                pageSize.Set(defaultPageSize);

            if (pageSize.Value > maxPageSize)
                pageSize.Set(maxPageSize);

            if (pageNo.Value < 1)
            {
                msg = "页码不能小于1";
                return false;
            }

            if (pageSize.Value < 1)
            {
                msg = "每页项数量不能小于1";
                return false;
            }

            msg = "";
            return true;

        }

    }
}
